package io.xiaojl.xj2ee.common.kit;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * Title: UserAgentKit
 * </p>
 * <p>
 * Description: 客户端工具类，用于判断是移动端还PC端
 * </p>
 *
 * @author jilong.xiao
 * @date 2018年6月13日
 */
public class UserAgentKit {
	public static Logger log = LoggerFactory.getLogger(UserAgentKit.class);

	/**
	 * 判断 移动端/PC端
	 */
	public static boolean isMobile(HttpServletRequest request) {
		List<String> mobileAgents = Arrays.asList("ipad", "iphone os", "rv:1.2.3.4", "ucweb", "android", "windows ce",
				"windows mobile", "micromessenger");
		String ua = request.getHeader("User-Agent").toLowerCase();
		for (String sua : mobileAgents) {
			if (ua.indexOf(sua) > -1) {
				log.info("该客户端属于[移动端]，具体客户端信息：",ua);
				return true;// 手机端
			}
		}
		log.info("该客户端属于[PC端]，具体客户端信息：", ua);
		return false;// PC端
	}

	/**
	 * 是否微信浏览器
	 */
	public static boolean isWechat(HttpServletRequest request) {
		String ua = request.getHeader("User-Agent").toLowerCase();
		if (ua.indexOf("micromessenger") > -1) {
			log.info("该客户端是[微信浏览器]？YES，具体客户端信息：",ua);
			return true;// 微信
		}
		log.info("该客户端是[微信浏览器]？NO，具体客户端信息：", ua);
		return false;// 非微信手机浏览器
	}
	
	public static void main(String[] args) {
		List<String> mobileAgents = Arrays.asList("ipad", "iphone os", "rv:1.2.3.4", "ucweb", "android", "windows ce",
				"windows mobile");
		String ua = "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/65.0.3325.146 safari/537.36"
				.toLowerCase();
		for (String sua : mobileAgents) {
			if (ua.indexOf(sua) > -1) {
				System.out.println("移动端");
				return;
			}
		}
		System.out.println("PC端");
	}
}